package com.linzm.leetcode.primary.exercises3_20230122;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author zimingl
 * @Date 2023/1/24 13:07
 * @Description: 存在重复元素 II
 */
public class Demo12_219 {
    /**
     * 给你一个整数数组 nums 和一个整数 k ，判断数组中是否存在两个 不同的索引 i 和 j ，
     * 满足 nums[i] == nums[j] 且 abs(i - j) <= k 。如果存在，返回 true ；否则，返回 false 。
     * 输入：nums = [1,2,3,1], k = 3
     * 输出：true
     */
    public static void main(String[] args) {
        Demo12_219 demo12_219 = new Demo12_219();
//        int[] nums = {1,2,3,1};
        int[] nums = {1,0,1,1};
//        int[] nums = {313,64,612,515,412,661,244,164,492,744,233,579,62,786,342,817,838,396,230,79,570,702,124,727,586,542,919,372,187,626,869,923,103,932,368,891,411,125,724,287,575,326,88,125,746,117,363,8,881,441,542,653,211,180,620,175,747,276,832,772,165,733,574,186,778,
//                586,601,165,422,956,357,134,857,114,450,64,494,679,495,205,859,136,477,879,940,139,903,689,954,335,859,78,20};
        int k = 1;
        boolean containsNearbyDuplicate = demo12_219.containsNearbyDuplicate3(nums, k);
        System.out.println(containsNearbyDuplicate);
    }

    private boolean containsNearbyDuplicate(int[] nums, int k) {
        if (nums.length == 1 || nums.length == 0) {
            return false;
        }
        Map<Integer, TreeSet<Integer>> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            TreeSet<Integer> orDefault = map.getOrDefault(nums[i], new TreeSet<>());
            orDefault.add(i);
            map.put(nums[i], orDefault);
        }
        List<TreeSet<Integer>> collect = map.values().stream().filter(set -> set.size() > 1).collect(Collectors.toList());
        if (collect.size() == 0) {
            return false;
        }
        for (TreeSet<Integer> treeSet : collect) {
            Integer[] integers = treeSet.stream().toArray(Integer[]::new);
            for (int i = 0; i < integers.length; i++) {
                for (int j = i + 1; j < integers.length; j++) {
                    if (Math.abs(integers[i] - integers[j]) <= k) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private boolean containsNearbyDuplicate2(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            int num = nums[i];
            if (map.containsKey(num) && i - map.get(num) <= k) {
                return true;
            }
            map.put(num, i);
        }
        return false;
    }

    public boolean containsNearbyDuplicate3(int[] nums, int k) {
        Set<Integer> set = new HashSet<>();
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            if (i > k) {
                set.remove(nums[i - k - 1]);
            }
            if (!set.add(nums[i])) {
                return true;
            }
        }
        return false;
    }
}
